<html>
<head>
    <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Java8日期API使用记录 | |ω･`)暗中观察</title>
<meta name="description" content="大爷不进来看看吗？这里有好康的哦~~~" />
<link rel="shortcut icon" href="https://Yanoona.github.io/favicon.ico">
<link rel="stylesheet" href="https://Yanoona.github.io/styles/main.css">

<script src="https://Yanoona.github.io/media/js/jquery.min.js"></script>
<script src="https://Yanoona.github.io/media/js/masonry.pkgd.min.js"></script>
<script src="https://Yanoona.github.io/media/js/aos.js"></script>
<script src="https://Yanoona.github.io/media/js/pace.min.js"></script>
<script src="https://Yanoona.github.io/media/js/view-image.min.js"></script>
<script src="https://Yanoona.github.io/media/js/functions.js"></script>
    <script src="https://Yanoona.github.io/media/js/waterfall.min.js"></script>
    <script src="https://Yanoona.github.io/media/js/prism.min.js"></script>
</head>
<body>
<header id="header" class="grid-container">
    <!-- start: .menu-wrapper -->
    <div class="menu-mobile">
        <i class="fa fa-reorder"></i>
    </div>
    <div class="menu-wrapper">
        <div class="">
            <nav class="main-nav " >
                <ul id="menu-header" class="menu gradient-effect">
                    
                        <li class="">
                            <a href="https://Yanoona.github.io" class="menu">
                                首页
                            </a>
                        </li>
                    
                        <li class="">
                            <a href="https://Yanoona.github.io/archives" class="menu">
                                归档
                            </a>
                        </li>
                    
                        <li class="">
                            <a href="https://Yanoona.github.io/tags" class="menu">
                                标签
                            </a>
                        </li>
                    
                        <li class="">
                            <a href="https://github.com/Yanoona" class="menu">
                                关于
                            </a>
                        </li>
                    
                </ul>
            </nav>
            <!-- end: .main-nav -->
            <div class="clear"></div>
            <div class="border hide-on-tablet hide-on-mobile"></div>
        </div>
        <div class="clear"></div>
    </div>
    <!-- end: .menu-wrapper -->
    <div class="clear"></div>
</header>

<main id="single" class="main grid-container fullcover no-sidebar aos-init aos-animate" data-aos="fade">

    <div class="center content">
        <div class="featured-image cover"
             style="background-image: url('https://Yanoona.github.io/post-images/lIPSlIjZp.jpg');">
            <div class="meta top">
                <time class="meta-info" style="float:left;" datetime="2020-05-11"><i
                            class="fa fa-calendar"></i><span
                            class="lately">2 个月前</span></time>
            </div>
            <div class="info">
                <div class="tags ">
                    
                        <a href="https://Yanoona.github.io/java/" class="ctag ctag-0 ctag-java"
                           aria-label="">Java</a>
                    
                </div>
                <h1 class="title ularge white bold">Java8日期API使用记录</h1>
            </div>
        </div>
    </div>

    <div class="epcl-page-wrapper">
        <div class="left-content grid-70 np-mobile">
            <article class="main-article post">
                <section class="post-content">
                    <div class="text">
                        <h2 id="日期转换">日期转换</h2>
<h3 id="1date转换成localdate">1.Date转换成LocalDate</h3>
<p><strong>这里的 Date 为 <code>java.util</code>包下的 Date。</strong></p>
<h4 id="第一种方式">第一种方式：</h4>
<!-- more -->
<pre><code class="language-java">Date date = new Date();
//使用Date类中的toInstant方法转换成Time包中的Instant类
Instant instant = date.toInstant();
//因为转换后的Instant类不带有时区，所以为其添加时区
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
//调用toLocalDate方法转换成LocalDate
zonedDateTime.toLocalDate();
</code></pre>
<h4 id="第二种方式">第二种方式：</h4>
<pre><code class="language-java">Date date = new Date();
//先将其转换成 java.sql包的Date，因为此包下的Date类中带有 直接转换LocalDate 的方法
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
LocalDate localDate = sqlDate.toLocalDate();
</code></pre>
<hr>
<h3 id="2date转换成localdate">2.Date转换成LocalDate</h3>
<p><strong>这里的 Date 为 <code>java.sql</code>包下的 Date。</strong></p>
<pre><code class="language-java">Date sqlDate = new Date(date.getTime());
LocalDate localDate = sqlDate.toLocalDate();
</code></pre>
<hr>
<h3 id="3timestamp转换为localdate">3.Timestamp转换为LocalDate</h3>
<pre><code class="language-java">Timestamp timestamp = new Timestamp(System.currentTimeMillis());
LocalDateTime localDateTime = timestamp.toLocalDateTime();
LocalDate localDate = localDateTime.toLocalDate();
</code></pre>
<h2 id="-more-"><!-- more --></h2>
<h4 id="4calendar转换为zonedatetime">4.Calendar转换为ZoneDateTime</h4>
<pre><code class="language-java">Calendar calendar = Calendar.getInstance();
//Calendar对象在Java1.1版本开始提供了一个方法用于获取时区对象getTimeZone方法
//转换需要先获取到时区信息
TimeZone timeZone = calendar.getTimeZone();
//自Java1.8版本开始，TimeZone类加入了一个方法可以获取到ZoneId
ZoneId zoneId = timeZone.toZoneId();
//使用ofInstant方法将Instant参数和时区封装成ZoneDateTime
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(calendar.toInstant(),zoneId);
</code></pre>
<h2 id="日期的解析与格式化控制">日期的解析与格式化控制</h2>
<blockquote>
<p><code>SimpleDateFormat</code> 类是线程不安全的，所以 Java1.8 版本加入了 <code>DateTimeFormatter</code>.</p>
<p><code>DateTimeFormatter</code> 类提供了大量预定义格式化器，包括常量（如ISO_LOCAL_DATE），模式字母（如yyyy-MM-dd）以及本地化样式.</p>
<p><strong>与<code>SimpleDateFormat</code> 不同的是，新版本的日期/时间API的格式化与解析不需要在创建转换器对象再进行转换了，通过时间日期对象的<code>parse/format</code>方法可以直接进行转换.</strong></p>
</blockquote>
<h3 id="1格式控制-format-和解析-parse">1.格式控制 format 和解析 parse</h3>
<pre><code class="language-java">LocalDateTime localDateTime = LocalDateTime.now();
//使用format方法进行格式化处理
String dateTime = localDateTime.format(DateTimeFormatter.ISO_DATE);
System.out.println(dateTime);

//使用parse方法解析字符串为日期对象
//注意：此处的dateTime字符串经过上面的格式化的处理后是LocalDate格式.
//如果使用LocalDateTime接收，编译会通过，但会报出异常：
//		Process 'command 'C:/Java/jdk1.8.0_211/bin/java.exe'' finished with non-zero exit value 1
LocalDate localDate = LocalDate.parse(dateTime);
System.out.println(localDate);
</code></pre>
<hr>
<h3 id="2使用-oflocalizeddate-方法指定解析长度">2.使用 ofLocalizedDate 方法指定解析长度</h3>
<p><strong>此方法在不同时区有不同的解析格式</strong></p>
<pre><code class="language-java">String dateTime1 = localDateTime.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
String dateTime2 = localDateTime.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
String dateTime3 = localDateTime.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
String dateTime4 = localDateTime.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
//输出结果：FULL：2020年5月12日 星期二
System.out.println(&quot;FULL：&quot; + dateTime1);
//输出结果：SHORT：20-5-12
System.out.println(&quot;SHORT：&quot; + dateTime2);
//输出结果：MEDIUM：2020-5-12
System.out.println(&quot;MEDIUM：&quot; + dateTime3);
//输出结果：LONG：2020年5月12日
System.out.println(&quot;LONG：&quot; + dateTime4);
</code></pre>
<hr>
<h3 id="3自定义格式">3.自定义格式</h3>
<pre><code class="language-java">String datetime = localDateTime.format(DateTimeFormatter.ofPattern(&quot;yyyy/MM/dd HH:mm:ss:SSS&quot;));
//输出结果：LONG：2020/05/12 09:43:25:213
System.out.println(datetime);
</code></pre>

                    </div>
                    <div class="clear"></div>
                </section>
            </article>
            <div class="clear"></div>

            <section class="related section">
                
                    <article class="prev grid-50 tablet-grid-50 grid-parent">
                        <div class="thumb cover lazy loaded"
                             style="background-image: url('https://Yanoona.github.io/post-images/CpjS5YWXn.jpg');"></div>
                        <a href="https://Yanoona.github.io/CpjS5YWXn/" class="full-link"></a>
                        <div class="info">
                            <time datetime="2020-05-13">2020-05-13</time>
                            <h4 class="title white no-margin">Spring 知识回顾之 概念</h4>
                        </div>
                        <span class="epcl-button red">
                  <img src="https://Yanoona.github.io/media/images/left-arrow.svg" width="15" alt="Left Arrow">
                </span>
                        <div class="overlay"></div>
                    </article>
                
                
                    <article class="next grid-50 tablet-grid-50 grid-parent">
                        <div class="thumb cover lazy loaded"
                             style="background-image: url('https://Yanoona.github.io/post-images/XvCd5eo8U.jpg');"></div>
                        <a href="https://Yanoona.github.io/XvCd5eo8U/" class="full-link"></a>
                        <div class="info">
                            <time datetime="2020-02-27">2020-02-27</time>
                            <h4 class="title white no-margin">CentOS 7定时执行Java程序</h4>
                        </div>
                        <span class="epcl-button red">
                  <img src="https://Yanoona.github.io/media/images/right-arrow.svg" width="15" alt="Left Arrow">
                </span>
                        <div class="overlay"></div>
                    </article>
                

                <div class="clear"></div>
            </section>
        </div>
    </div>
</main>

    <footer id="footer" class="grid-container">
        <div class="widgets row gradient-effect">
            <div class="default-sidebar border-effect">
              <div class="grid-33 tablet-grid-50 mobile-grid-100">
                <section id="tag_cloud-2" class="widget widget_epcl_posts_thumbs underline-effect">
                  <h4 class="widget-title title white bordered">最新文章</h4>
                  
                  
                  <article class="item post-0 post type-post status-publish format-standard has-post-thumbnail hentry">
                    <a href="https://Yanoona.github.io/2rO0MFRF0/" class="thumb hover-effect">
                      <span class="fullimage cover" style="display: block;background-image: url('https://Yanoona.github.io/post-images/2rO0MFRF0.jpg');"></span>
                    </a>
                    <div class="info gradient-effect">
                      <time datetime="2020-06-29">2020-06-29</time>
                      <h4 class="title usmall">
                        <a href="https://Yanoona.github.io/2rO0MFRF0/">Java8 Stream流学习笔记</a>
                      </h4>
                    </div>
                    <div class="clear"></div>
                  </article>
                  
                  
                  
                  <article class="item post-1 post type-post status-publish format-standard has-post-thumbnail hentry">
                    <a href="https://Yanoona.github.io/ta0wVkBzF/" class="thumb hover-effect">
                      <span class="fullimage cover" style="display: block;background-image: url('https://Yanoona.github.io/post-images/ta0wVkBzF.jpg');"></span>
                    </a>
                    <div class="info gradient-effect">
                      <time datetime="2020-05-13">2020-05-13</time>
                      <h4 class="title usmall">
                        <a href="https://Yanoona.github.io/ta0wVkBzF/">Spring 知识回顾之 体系结构</a>
                      </h4>
                    </div>
                    <div class="clear"></div>
                  </article>
                  
                  
                  
                  <article class="item post-2 post type-post status-publish format-standard has-post-thumbnail hentry">
                    <a href="https://Yanoona.github.io/CpjS5YWXn/" class="thumb hover-effect">
                      <span class="fullimage cover" style="display: block;background-image: url('https://Yanoona.github.io/post-images/CpjS5YWXn.jpg');"></span>
                    </a>
                    <div class="info gradient-effect">
                      <time datetime="2020-05-13">2020-05-13</time>
                      <h4 class="title usmall">
                        <a href="https://Yanoona.github.io/CpjS5YWXn/">Spring 知识回顾之 概念</a>
                      </h4>
                    </div>
                    <div class="clear"></div>
                  </article>
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  <div class="clear"></div>
                </section>
              </div>

              <div class="grid-33 tablet-grid-50 mobile-grid-100">
                <section id="tag_cloud-2" class="widget widget_tag_cloud underline-effect">
                  <h4 class="widget-title title white bordered">标签云</h4>
                  <div class="tagcloud">
                    
                      <a href="https://Yanoona.github.io/java/" class="ctag ctag-0 ctag-java" aria-label="">Java</a>
                    
                      <a href="https://Yanoona.github.io/7D-9V5P5T/" class="ctag ctag-1 ctag-7D-9V5P5T" aria-label="">Spring</a>
                    
                      <a href="https://Yanoona.github.io/wDtcCNomO/" class="ctag ctag-2 ctag-wDtcCNomO" aria-label="">Crontab定时任务</a>
                    
                      <a href="https://Yanoona.github.io/j_dWapwnj/" class="ctag ctag-3 ctag-j_dWapwnj" aria-label="">CentOS 7</a>
                    
                      <a href="https://Yanoona.github.io/dXBZlTYs0/" class="ctag ctag-4 ctag-dXBZlTYs0" aria-label="">Vue</a>
                    
                      <a href="https://Yanoona.github.io/bigData/" class="ctag ctag-5 ctag-bigData" aria-label="">大数据</a>
                    
                      <a href="https://Yanoona.github.io/Sqoop/" class="ctag ctag-6 ctag-Sqoop" aria-label="">Sqoop</a>
                    
                      <a href="https://Yanoona.github.io/Hive/" class="ctag ctag-7 ctag-Hive" aria-label="">Hive</a>
                    
                      <a href="https://Yanoona.github.io/mysql/" class="ctag ctag-8 ctag-mysql" aria-label="">mysql</a>
                    
                      <a href="https://Yanoona.github.io/Hadoop/" class="ctag ctag-9 ctag-Hadoop" aria-label="">Hadoop</a>
                    
                  </div>
                  <div class="clear"></div>
                </section>
              </div>

              <div class="grid-33 tablet-grid-50 mobile-grid-100">
                <section id="epcl_about-2" class="widget widget_epcl_about underline-effect">
                  <h4 class="widget-title title white bordered">关于我</h4>
                  <div class="avatar">
                    <a href="https://github.com/Yanoona" class="translate-effect thumb"><span class="fullimage cover" style="background-image: url(https://Yanoona.github.io/images/avatar.png);"></span></a>
                  </div>
                  <div class="info">
                    <h4 class="title small author-name gradient-effect no-margin"><a href="https://github.com/Yanoona">|ω･`)暗中观察</a></h4>
                    <p class="founder">大爷不进来看看吗？这里有好康的哦~~~</p>
                    <div class="social">
                      
                        
                            <a href="https://github.com/Yanoona" class="translate-effect" target="_blank"><i class="fa fa-github"></i></a>
                        
                      
                        
                      
                        
                            <a href="https://blog.csdn.net/qq_39299449" class="translate-effect" target="_blank"><i class="fa fa-csdn"></i></a>
                        
                      
                        
                            <a href="https://www.jianshu.com/u/6d8fc7359702" class="translate-effect" target="_blank"><i class="fa fa-jianshu"></i></a>
                        
                      
                    </div>
                  </div>
                  <div class="clear"></div>
                  </section>
              </div>

            </div>
            <div class="clear"></div>
        </div>

        <p class="published border-effect">
          ©2020 共 12 篇文章
        </p>
        
        <a href="javascript:void(0)" id="back-to-top" class="epcl-button dark" style="display:none">
          <i class="fa fa-arrow"></i>
        </a>
    </footer>
    <div class="clear"></div>

<script type="text/javascript">
    jQuery(document).ready(function ($) {
        $("photos img").each(function () {
            var _a = $("<a></a>").attr("href", this.src);
            $(this).wrap("<div class='photo'></div>").wrap(_a);
        })
        $("figure img").each(function () {
            var _b = $("<a></a>").attr("href", this.src);
            $(this).wrap(_b);
        })
        isImgLoad(function () {
            var photos = document.querySelector('photos');
            if (photos) {
                waterfall(photos);
            }
            $(window).resize(function () {
                if (photos) {
                    waterfall(photos);
                }
            });
        });
        var t_img;
        var isLoad = true;

        function isImgLoad(callback) {
            $('photos img').each(function () {
                if (this.height === 0) {
                    isLoad = false;
                    return false;
                }
            });
            if (isLoad) {
                clearTimeout(t_img);
                callback();
            } else {
                isLoad = true;
                t_img = setTimeout(function () {
                    isImgLoad(callback);
                }, 500);
            }
        }
    });
</script>

</div>
<!-- end: #wrapper -->
</body>
</html>
